package com.yeraphael.controller;


import com.yeraphael.utils.AliOSSProperties;
import com.yeraphael.entity.Result;
import com.yeraphael.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@Slf4j
public class UploadController {

    @Autowired
    private AliOSSProperties aliOSSProperties;

    @PostMapping("/upload")
    public Result upload( @RequestParam("file") MultipartFile file) throws Exception {
//        log.info("{},{},{}", username, age, file);

        String originalFilename = file.getOriginalFilename();
        log.info("原始文件名：{}", originalFilename);
//        通过UUID生成随机字符串
        String extName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        存储在本地
//        file.transferTo( new File("D:/"+newFileName));

//        上传到云端
        String url = AliyunOSSUtils.upload(aliOSSProperties.getEndpoint(), aliOSSProperties.getBucketName(), file.getBytes(), extName);

        return Result.success(url);
    }
}
